package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.AccountData;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

public class FriendSearchAdapter extends BaseAdapter {
    private Context mContext;
    private List<AccountData> dataList;

    public FriendSearchAdapter(Context ctx) {
        mContext = ctx;
        if (null == dataList) {
            dataList = new ArrayList<AccountData>();
        }
    }

    public void setData(List<AccountData> list) {
        if (null == dataList) {
            dataList = new ArrayList<AccountData>();
        }
        dataList = list;
    }

    public void addData(List<AccountData> list) {
        if (null != dataList && null != list) {
            for (int i = 0; i < list.size(); i++) {
                dataList.add(list.get(i));
            }
        }
    }

    public void cleardata() {
        if (null != dataList) {
            dataList.clear();
        }
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shield_list_item, null);
            holder = new ViewHolder();
            holder.headIV = (ImageView) convertView.findViewById(R.id.shiled_imageview);
            holder.nameTV = (TextView) convertView.findViewById(R.id.shield_name);
            holder.labelTV = (TextView) convertView.findViewById(R.id.shield_label);
            holder.sexIV = (ImageView) convertView.findViewById(R.id.shield_sex);
            holder.addV = (ImageView) convertView.findViewById(R.id.shield_isIndustry);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        AccountData data = dataList.get(position);
        if (data != null) {
            PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.getHeadPic());
            holder.nameTV.setText(data.getRealName() == null ? "" : data.getRealName());
            holder.labelTV.setText(data.getLabel() == null ? "" : data.getLabel());
            Utils.showSex(mContext, data.getSex(), holder.sexIV);
            if (data.getIsIndustryAuthority()) {
                holder.addV.setVisibility(View.VISIBLE);
            } else {
                holder.addV.setVisibility(View.GONE);
            }

        }
        return convertView;
    }

    private class ViewHolder {
        public ImageView headIV;
        public TextView nameTV;
        public TextView labelTV;
        public ImageView sexIV;
        public ImageView addV;
    }
}
